package com.appprogram.baseapppackage.util;

import android.annotation.SuppressLint;

import com.appprogram.baseapppackage.entity.UploadEntity;

import java.io.File;

import rxhttp.wrapper.param.RxHttp;

/**
 * Created by Android Studio .
 * Project：SixPulsesPackage com.sixpulsespackage.union.fun
 * Date：2019/5/29 11:23
 * Introduce：SixPulsesPackage
 */
public class ImageUpLoad {
    private static ImageUpLoad sImageUpLoad;

    public static ImageUpLoad getInstance() {
        if (null == sImageUpLoad) sImageUpLoad = new ImageUpLoad();
        return sImageUpLoad;
    }

    /**
     * 固定地址上传图片
     */
    @SuppressLint("CheckResult")
    public void uploadImage(String path, ImageCall zCallParamDelay, int id) {
        RxHttp.postForm("")
                .addFile("image", new File(path))
                .setDomainToUploadIfAbsent()
                .asResponse(UploadEntity.class)
                .subscribe(pUploadEntityMutableLiveData -> {
                    UploadEntity uploadEntity = pUploadEntityMutableLiveData.getValue();
                    if (null != uploadEntity) {
                        zCallParamDelay.call(uploadEntity.getResult_data(), uploadEntity.getResult_info());
                    }
                });
//        EasyHttp.post(Constants.UPLOAD_IMAGE)
//                //.addFileParams("image", files, null)
//                .params("image", new File(path), null)
//                .execute(new SimpleCallBack<String>() {
//                    @Override
//                    public void onSuccess(String s) {
//                        if (null != s) {
//                            UploadEntity uploadEntity = new GsonBuilder().create().fromJson(s, UploadEntity.class);
//                            zCallParamDelay.call(uploadEntity.getResult_data(), uploadEntity.getResult_info());
//                        }
//                    }
//                });
    }

    /**
     * 后台动态获取上传图片地址
     *
     * @param path    图片文件路径
     * @param httpUrl 上传图片网络地址
     */
    @SuppressLint("CheckResult")
    public void uploadImage(String path, String httpUrl, ImageCall zCallParamDelay, int id) {
        RxHttp.postForm(httpUrl)
                .addFile("image", new File(path))
                //.setDomainToUploadIfAbsent()
                .asResponse(UploadEntity.class)
                .subscribe(pUploadEntityMutableLiveData -> {
                    UploadEntity uploadEntity = pUploadEntityMutableLiveData.getValue();
                    if (null != uploadEntity) {
                        zCallParamDelay.call(uploadEntity.getResult_data(), uploadEntity.getResult_info());
                    }
                });
        //        EasyHttp.post(httpUrl)
//                //.addFileParams("image", files, null)
//                .params("image", new File(path), null)
//                .execute(new SimpleCallBack<String>() {
//                    @Override
//                    public void onSuccess(String s) {
//                        if (null != s) {
//                            UploadEntity uploadEntity = new GsonBuilder().create().fromJson(s, UploadEntity.class);
//                            zCallParamDelay.call(uploadEntity.getResult_data(), uploadEntity.getResult_info());
//                        }
//                    }
//                });
    }

    public interface ImageCall {
        void call(String resultData, String resultInfo);
    }
}

